import sys, os, time, re

def usage():
    print("please give me a raw data file following me and a platform name.")
    print("like this:\n\textlog.py rawfile mx31")
    print("Enjoy!!!")

if len(sys.argv) < 3:
    usage()
    sys.exit(1)

if not os.path.exists(sys.argv[1]):
    print("I could not find %s.\nAre you sure about the path?" % sys.argv[1])
    sys.exit(1)

outputdir = sys.argv[2]
cetkoutput = outputdir+"/cetk"
noncetkoutput = outputdir+"/non_cetk"

if not os.path.isdir(outputdir):
    os.mkdir(outputdir)
if not os.path.isdir(cetkoutput):
    os.mkdir(cetkoutput)
if not os.path.isdir(noncetkoutput):
    os.mkdir(noncetkoutput)

filename = sys.argv[1]
#f = open(filename, "r", encoding="utf-8")
f = open(filename, "r")
isvalidate = False
iswrite = False
isskipped = False
isfailed = False
isaborted = False
iscetk = False

while True:
    line = f.readline()
    if len(line) == 0:
        if f.tell() < os.path.getsize(filename):
            continue
        if iswrite:
            logf.close()
            if iscetk:
                resultname = cetkoutput + "/" + casename + "_HUNG.log"
            else:
                resultname = noncetkoutput + "/" + casename + "_NotCETK.log"
            print(logname, resultname)
            if os.path.exists(resultname):
                os.unlink(resultname)
            os.rename(logname, resultname)
        if not isvalidate:
            """no such "call" string in the raw file"""
            print("Please make sure the raw file has been generated according to the \nrequirements which should begin a case log with the statement of \"call xxxxx.bat\"")
        break
# find the start and log file name if it exists
# the log file name should start with "call xxxxxxxx.bat"
    if re.search("^call .*\.bat", line):
        print(line)
        if iswrite:
            iswrite = False
            logf.close()
            resultname = noncetkoutput + "/" + casename + "_NotCETK.log"
            if os.path.exists(resultname):
                os.unlink(resultname)
            os.rename(logname, resultname)
        lognameline = line.strip().split("call")
        nameline = lognameline[1].strip()
        nameline = nameline.strip('"').strip('.').split('.')
        casename = re.sub(r'\\.*\\', '', nameline[0])
        logname = outputdir+"/"+casename + ".log"
        os.path.normpath(logname)
        if os.path.exists(logname):
            os.unlink(logname)
        logf = open(logname, "w")
        isvalidate = True
        iswrite = True
    #else:
    #    """no such "call" string in the raw file"""
    #    print("Please make sure the raw file has been generated according to the \nrequirements which should begin a case log with the statement of \"call xxxxx.bat\"")
    #    f.close()
    #    sys.exit(2)

    if iswrite:
        logf.writelines(line)
        if not iscetk:
            if re.search("<TESTGROUP>", line):
                iscetk = True
            continue
        if re.search("^\s*\*\*\* Failed:\s+", line):
            resultline = line.strip().split(':')
            result = resultline[1].strip()
            if int(result) > 0: 
                isfailed = True 
            else: 
                isfailed = False

        if re.search("^\s*\*\*\* Skipped:\s+", line):
            resultline = line.strip().split(':')
            result = resultline[1].strip()
            if int(result) > 0: 
                isskipped = True 
            else: 
                isskipped = False

        if re.search("^\s*\*\*\* Aborted:\s+", line):
            resultline = line.strip().split(':')
            result = resultline[1].strip()
            if int(result) > 0: 
                isaborted = True 
            else: 
                isaborted = False
        
# find the end
# the log content should end with "</TESTGROUP>"
        if re.search("^</TESTGROUP>", line):
            iswrite = False
            logf.close()
            resultname = cetkoutput + "/" + casename
            resultname = os.path.normpath(resultname)
            if isfailed:
                resultname = resultname + "_FAILED" 
            if isskipped:
                resultname = resultname + "_SKIPPED" 
            if isaborted:
                resultname = resultname + "_ABORTED" 
            if not isfailed and not isskipped and not isaborted:
                resultname = resultname + "_PASSED.log"
            else:
                resultname = resultname + ".log"
            if os.path.exists(resultname):
                os.unlink(resultname)
            print(logname, resultname)
            os.rename(logname, resultname)
            isfailed = False
            isskipped = False
            isaborted = False
            iscetk = False
        continue

# the log content should begin with "<TESTGROUP>"
    #if re.search("^<TESTGROUP>", line):
    #    iscetk = True
    #elif iswrite:
    #    logf.close()
    #    resultname = casename+"_HUNG.log"
    #    os.rename(logname, resultname)
    #else:
    #    """no such "<TESTGROUP>" string in the raw file"""
    #    print("Please make sure the raw file includes the CETK log data")
    #    f.close()
    #    sys.exit(2)
print("done!")
f.close();

